package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.dto.CasesBasicDto;
import com.jiantong.lawEnforcing.enums.LawCasesStatus;
import com.jiantong.lawEnforcing.impl.manager.CasesBasicManager;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.model.SimplenessCases;
import com.jiantong.lawEnforcing.service.CasesBasicService;
import com.jiantong.lawEnforcing.service.PartiesInformationService;
import com.jiantong.lawEnforcing.service.SimplenessCasesService;
import com.jiantong.lawEnforcing.util.LawUtil;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * 案件信息
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class CasesBasicApis {

    @RpcConsumer
    private CasesBasicService casesBasicService;
    @RpcConsumer
    private PartiesInformationService partiesInformationService;

    @RpcConsumer
    private SimplenessCasesService simplenessCasesService;

    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.casesBasic.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public CasesBasic findById(Long id){
        Response<CasesBasic> response = casesBasicService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find casesBasic read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 根据id查询casesBasicDto
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.casesBasicDto.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public CasesBasicDto findCasesBasicDtoById(Long id){
        Response<CasesBasicDto> response = casesBasicService.finCasesBasicDtoById(id);
        if (!response.isSuccess()){
            log.error("failed to find casesBasicDto read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
/*

    */
/**
     * 根据id查询casesBasicDto
     * @param id
     * @return
     *//*

    @OpenMethod(
            key = "find.casesBasicDto.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public CasesBasicDto findCasesBasicDto(Long id){
        CasesBasic casesBasic = findById(id);
        Response<PartiesInformation> response = partiesInformationService.finById(casesBasic.getParties_id());
        if (!response.isSuccess()){
            log.error("failed to find partiesInformation read by id = {}, cause : {}", findById(id).getParties_id(), response.getError());
            throw new OPServerException(response.getError());
        }
        CasesBasicDto casesBasicDto = new CasesBasicDto();
        casesBasicDto.setPartiesInformation(response.getResult());
        casesBasicDto.setCasesBasic(casesBasic);
        return casesBasicDto;
    }

*/

    /**
     * 创建一条信息
     * @param casesBasic
     * @return
     */
    @OpenMethod(
            key = "create.casesBasic",
            paramNames = {"casesBasic"},
            httpMethods = RequestMethod.POST
    )
    public Long create(CasesBasic casesBasic){
        Response<Long> response = casesBasicService.create(casesBasic);
        if(!response.isSuccess()){
            log.error("failed to create casesBasic read by casesBasic = {}, cause : {}", casesBasic, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 创建一条信息  利用事务创建一个当事人, 根据当事人id创建案件表
     * @param casesBasicDto
     * @return
     */
    @OpenMethod(
            key = "create.casesBasicDto",
            paramNames = {"casesBasicDto"},
            httpMethods = RequestMethod.POST
    )
    public Long createCasesBasic(CasesBasicDto casesBasicDto){

      Response<Long> response = casesBasicService.createCasesBasicDto(casesBasicDto);
      if (!response.isSuccess()){
          log.error("failed to create casesBasic read by casesBasicDto = {}, cause : {}", casesBasicDto, response.getError());
          throw new OPServerException(response.getError());
      }
      return response.getResult();
    }

    /**
     * 更新单据
     * @param casesBasic
     * @return
     */
    @OpenMethod(
            key = "update.casesBasic",
            paramNames = {"casesBasic"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(CasesBasic casesBasic){
        if(casesBasic.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = casesBasicService.update(casesBasic);
        if(!response.isSuccess()){
            log.error("failed to update casesBasic");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(), "failed to update casesBasic");
        }
        return response.getResult();
    }


    /**
     * 更新单据
     * @param casesBasicDto
     * @return
     */
    @OpenMethod(
            key = "update.casesBasicDto",
            paramNames = {"casesBasicDto"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean updateDto(CasesBasicDto casesBasicDto){
        Boolean b = update(casesBasicDto.getCasesBasic());
        if(casesBasicDto.getPartiesInformation().getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = partiesInformationService.update(casesBasicDto.getPartiesInformation());
        if (!response.isSuccess()){
            log.error("failed to update partiesInformation");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(), "failed to update partiesInformation");
        }

        return response.isSuccess();
    }

    /**
     * 根据状态和案由查询案件信息
     * @param pageNo
     * @param pageSize
     * @param status
     * @param cause_action
     * @return
     */
    @OpenMethod(
            key = "find.paging.CasesBasic",
            paramNames = {"pageNo","pageSize","status","cause_action","parties_type","parties_name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<CasesBasic> pagingCasesBasic(
            Integer pageNo,
            Integer pageSize,
            Integer status,
            String cause_action,
            Integer parties_type,
            String parties_name
    ){
        Response<Paging<CasesBasic>> pagingResponse =casesBasicService.pagingByStatus(pageNo,pageSize,status,cause_action,parties_type,parties_name);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging casesBasic status {}, cause : {}", status, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    /**
     * 查询案件, 根据案件表中的当事人id 组成dto
     * @param pageNo
     * @param pageSize
     * @param status   状态: 0,暂存(未规定案件类型); 1,已指派; 2已接收指派;
     * @param cause_action 案由
     * @param parties_type  当事人(1为个人; 2为单位)
     * @param parties_name 当事人名字
     * @return
     */
    @OpenMethod(
            key = "find.paging.CasesBasicDto",
            paramNames = {"pageNo","pageSize","status","cause_action","parties_type","parties_name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<CasesBasicDto> pagingCasesBasicDto(
            Integer pageNo,
            Integer pageSize,
            Integer status,
            String cause_action,
            Integer parties_type,
            String parties_name
            ){

        Response<Paging<CasesBasicDto>> pagingResponse = casesBasicService.pagingByList(pageNo,pageSize,status,cause_action,parties_type,parties_name);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging CasesBasicDto status {}, cause : {}", status, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();

    }
}

